<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:c="http://java.sun.com/jstl/core"
  xmlns:nxu="http://nuxeo.org/nxweb/util"
  xmlns:nxh="http://nuxeo.org/nxweb/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  rendered="#{widgetProperty_enabled or widgetProperty_disableFiltered}">

<nxu:set var="fancyReRenderId"
  value="#{widget.id}_ajax_panel"
  cache="true">
<nxu:set var="elementsToReRender"
  value="#{nxu:joinRender(ajaxReRender, fancyReRenderId)}">

<c:if test="#{widget.mode != 'after_view' and widget.mode != 'tab_content'}">

  <c:if test="#{empty widgetProperty_iframe}">

    <c:if test="#{not widgetProperty_useButton}">
      <h:commandLink id="#{widget.id}_link"
        styleClass="#{widgetProperty_actionStyleClass} #{widgetProperty_styleClass}"
        disabled="#{!widgetProperty_available}"
        accesskey="#{widgetProperty_accessKey}">
        <f:attribute name="selectedValue" value="#{widgetProperty_actionId}" />
        <f:attribute name="targetComponentId" value="#{widgetProperty_clickedActionIdHolder}" />
        <a4j:ajax execute="@this" render="#{elementsToReRender}" listener="#{selectionActions.setStaticValue}"/>
        <ui:include src="/incl/action/widgets/action_icon_label_template.xhtml" />
        <ui:insert name="inside_action" />
      </h:commandLink>
    </c:if>

    <c:if test="#{widgetProperty_useButton}">
      <h:commandButton id="#{widget.id}_link"
        onclick="#{widgetProperty_onclick}"
        value="#{messages[widget.label]}"
        styleClass="#{widgetProperty_actionStyleClass} #{widgetProperty_styleClass}"
        disabled="#{!widgetProperty_available}"
        accesskey="#{widgetProperty_accessKey}">
        <f:attribute name="selectedValue" value="#{widgetProperty_actionId}" />
        <f:attribute name="targetComponentId" value="#{widgetProperty_clickedActionIdHolder}" />
        <a4j:ajax execute="@this" render="#{elementsToReRender}" listener="#{selectionActions.setStaticValue}"/>
        <ui:insert name="inside_action" />
      </h:commandButton>
    </c:if>

  </c:if>

  <c:if test="#{not empty widgetProperty_iframe}">

    <ui:insert name="fancybox_properties_include" />

    <nxu:set var="fancyboxJs"
      value="openFancyBox('#{nxu:javaScriptEscape(widgetProperty_iframe)}', {
              width: '#{widgetProperty_width}',
              height: '#{widgetProperty_height}',
              scrolling: '#{widgetProperty_scrolling}',
              autoScale: #{widgetProperty_autoScale == null ? true : widgetProperty_autoScale},
              autoDimensions: #{widgetProperty_autoDimensions == null ? true : widgetProperty_autoDimensions},
              modal: #{widgetProperty_modal == null ? false : widgetProperty_modal},
              centerOnScroll: #{widgetProperty_centerOnScroll == null ? true : widgetProperty_centerOnScroll},
              enableEscapeButton: #{widgetProperty_enableEscapeButton == null ? true : widgetProperty_enableEscapeButton},
              onClosed: function() {
                var theFunction = '#{widgetProperty_onClosed}';
                if (theFunction.length !== 0) {
                  window[theFunction]();
                }
              }
            });">

      <c:if test="#{not widgetProperty_useButton}">
        <a4j:commandLink id="#{widget.id}_link"
          execute="@this"
          ignoreDupResponses="true" requestDelay="100"
          onclick="#{widgetProperty_onclick};#{fancyboxJs}"
          disabled="#{!widgetProperty_available}"
          styleClass="#{widgetProperty_actionStyleClass} #{widgetProperty_styleClass}"
          accesskey="#{widgetProperty_accessKey}">
          <ui:include src="/incl/action/action_icon_label_template.xhtml" />
          <ui:insert name="inside_action" />
        </a4j:commandLink>
      </c:if>

      <c:if test="#{widgetProperty_useButton}">
        <a4j:commandButton id="#{widget.id}_link"
          execute="@this"
          ignoreDupResponses="true" requestDelay="100"
          onclick="#{widgetProperty_onclick};#{fancyboxJs}"
          value="#{messages[widget.label]}"
          disabled="#{!widgetProperty_available}"
          styleClass="#{widgetProperty_actionStyleClass} #{widgetProperty_styleClass}"
          accesskey="#{widgetProperty_accessKey}">
          <ui:insert name="inside_action" />
        </a4j:commandButton>
      </c:if>

    </nxu:set>

  </c:if>

</c:if>

<c:if test="#{widget.mode == 'after_view' and empty widgetProperty_iframe}">

  <a4j:outputPanel id="#{widget.id}_ajax_panel" layout="block">

    <nxu:set var="fancyboxFormId"
      value="#{widget.id}_fancyform"
      cache="true">
    <nxu:set
      var="actionClicked"
      value="#{clickedActionId == action.id or nxu:hasMessages(fancyboxFormId)}"
      cache="false">

      <f:subview rendered="#{actionClicked}" id="#{widget.id}_fancy_subview">

        <h:form id="#{widget.id}_closeFancyboxForm">
          <a4j:jsFunction id="#{widget.id}_closeFancybox"
            name="#{widget.id}_closeFancybox"
            render="#{elementsToReRender}" execute="@this">
            <f:attribute name="selectedValue" value="" />
            <f:attribute name="targetComponentId" value="#{widgetProperty_clickedActionIdHolder}" />
            <nxu:actionListenerMethod value="#{selectionActions.setStaticValue}" />
          </a4j:jsFunction>
        </h:form>

        <ui:insert name="fancybox_properties_include" />

        <script>
          jQuery(document).ready(function() {
            openFancyBox('##{widget.id}_box', {
              width: '#{widgetProperty_width}',
              height: '#{widgetProperty_height}',
              scrolling: '#{widgetProperty_scrolling}',
              autoScale: #{widgetProperty_autoScale == null ? true : widgetProperty_autoScale},
              autoDimensions: #{widgetProperty_autoDimensions == null ? true : widgetProperty_autoDimensions},
              modal: #{widgetProperty_modal == null ? false : widgetProperty_modal},
              centerOnScroll: #{widgetProperty_centerOnScroll == null ? true : widgetProperty_centerOnScroll},
              enableEscapeButton: #{widgetProperty_enableEscapeButton == null ? true : widgetProperty_enableEscapeButton},
              onClosed: function() {
                var theFunction = '#{widgetProperty_onClosed}';
                if (theFunction.length !== 0) {
                  window[theFunction]();
                }
                #{widget.id}_closeFancybox();
              }
            });
          });
        </script>
        <div style="display:none">
          <div id="#{widget.id}_box">
            <c:if test="#{not empty widgetProperty_include}">
              <ui:include src="#{widgetProperty_include}" />
            </c:if>
          </div>

          <script type="text/javascript">
            (function() {
              jQuery(document).ready(function() {
                var content = jQuery('#fancybox-content .content'),
                  computedHeight = jQuery('#fancybox-content').height() || 0

                if (content !== null) {
                  computedHeight -= jQuery('#fancybox-content h3').outerHeight(true) || 0
                  computedHeight -= jQuery('#fancybox-content .buttonsGadget').outerHeight(true) || 0
                  computedHeight -= parseInt(content.css('padding-top'), 10)
                  computedHeight -= parseInt(content.css('padding-bottom'), 10)
                  computedHeight -= parseInt(content.css('margin-top'), 10)
                  computedHeight -= parseInt(content.css('margin-bottom'), 10)
                  content.height(computedHeight)
                }
              })
            })()
          </script>
        </div>
      </f:subview>

    </nxu:set>
    </nxu:set>

  </a4j:outputPanel>

</c:if>

</nxu:set>
</nxu:set>

</ui:composition>